// pages/tuibao-integral-mall/charity-form/charity-form.js
var WxParse = require("../../../wxParse/wxParse.js"), shoppingCart = require("../../../components/shopping_cart/shopping_cart.js"), specificationsModel = require("../../../components/specifications_model/specifications_model.js"), gSpecificationsModel = require("../../../components/goods/specifications_model.js"), goodsBanner = require("../../../components/goods/goods_banner.js"), goodsInfo = require("../../../components/goods/goods_info.js"), goodsBuy = require("../../../components/goods/goods_buy.js"), goodsRecommend = require("../../../components/goods/goods_recommend.js"), p = 1, is_loading_comment = !1, is_more_comment = !0, share_count = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      realname: '',
      sex: 1,
      mobile: '',
      remark: ''
    },
    sex: [{
      id: 0,
      name: '女'
    }, {
      id: 1,
      name: '男'
    }],
    
  },
  inputHandle(e){
    let key = 'form.' + e.currentTarget.dataset.key
    this.setData({
      [key]: e.detail.value
    })
  },
  formSubmit(){
    if (!this.data.form.realname){
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return false
    } 
    else if (!/1[0-9]{10}/.test(this.data.form.mobile)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return false
    } 
    getApp().request({
      url: getApp().api.sign_up.default_join,
      method: 'post',
      data: {
        ...this.data.form,
        id: this.data.goods_id,
      },
      success: function (t) {
        if (0 == t.code) {
          wx.showModal({
            title: '提示',
            content: t.msg,
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              } else if (res.cancel) {
                
              }
            }
          })
        }
        else {
          wx.showToast({
            title: t.msg,
            icon: 'none',
            duration: 3000
          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      goods_id: options.id
    })
    getApp().request({
      url: getApp().api.sign_up.default_join,
      data: {
        id: options.id
      },
      success: (res) => {
        if (res.code == 0) {
          WxParse.wxParse("detail", "html", res.data.activity.content, this);
          if (res.data.joinInfo && res.data.joinInfo.user_id){
            wx.showModal({
              title: '提示',
              content: res.msg,
              showCancel: false,
            })
          }
          this.setData({
            goods: res.data.activity,
            form: res.data.joinInfo || this.data.form
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})